<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="产品" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="partno">
              <j-popup
                v-model="model.partno"
                field="partno"
                org-fields="partno,partno,pn_group"
                dest-fields="part,partno,pnGroup"
                code="bom"
                :multi="true"
                @input="popupCallback"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="产品类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pType">
              <a-radio-group v-model="model.pType" button-style="solid" @change="pTypeChange">
                <a-radio-button value="1">产品</a-radio-button>
                <a-radio-button value="2">产品组</a-radio-button>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
          <!--          <a-col :span="12">-->
          <!--            <a-form-model-item label="版本" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rev">-->
          <!--              <a-input v-model="model.rev" placeholder="请输入版本" ></a-input>-->
          <!--            </a-form-model-item>-->
          <!--          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="配置类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag type="list" v-model="model.type" dictCode="qis_prochead_cfg@TYPE"
                                 placeholder="请选择配置类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>

import { httpAction } from '@/api/manage'

export default {
  name: 'QisProcheadCfgModal',
  components: {},
  data() {
    return {
      isEdit: false,
      recId: '',
      title: '操作',
      width: 800,
      visible: false,
      model: {
        pType: '1'
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      validatorRules: {
        recId: [
          { required: true, message: '请输入表号!' }
        ],
        serialno: [
          { required: true, message: '请输入编号!' }
        ],
        partno: [
          { required: true, message: '请输入产品!' }
        ],
        type: [
          { required: true, message: '请输入配置类型!' }
        ]
      },
      url: {
        add: '/qis/qisProcheadCfg/add',
        edit: '/qis/qisProcheadCfg/edit'
      }

    }
  },
  created() {
    // 备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    pTypeChange() {
      let pType = this.model.pType
      if (pType === '1') {
        this.model.partno = this.model.part
      } else {
        this.model.partno = this.model.pnGroup
      }
    },
    add() {
      this.isEdit = false
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.recId = this.model.recId
      if (this.model.recId) {
        this.isEdit = true
      }
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.$refs.form.clearValidate()
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.recId) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(row, value) {
      this.model = Object.assign(this.model, value)
      this.pTypeChange()
    }

  }
}
</script>
